package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2023-03-11
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
	/**
	 * 有条件的分页查询
	 */
	@Resource
	private EduTeacherService eduTeacherService;

	@Resource
	private EduTeacherMapper eduTeacherMapper;


	@Override
	public Page<EduTeacher> pageTeacherByCondition(Page<EduTeacher> page, TeacherQuery teacherQuery) {

		/*QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("sort");
		if(teacherQuery==null){
			eduTeacherService.page(page, null);
			return page;
		}
		String name = teacherQuery.getName();
		Integer level = teacherQuery.getLevel();
		String begin = teacherQuery.getBegin();
		String end = teacherQuery.getEnd();
		System.out.println(begin+end);

		queryWrapper.eq(level!=null,"Level", level);
		queryWrapper.like(name!=null,"Name", name);
		queryWrapper.ge(begin!=null,"gmt_create", begin);// >=
		queryWrapper.le(end!=null,"gmt_create", end);// =<
		eduTeacherService.page(page,queryWrapper);
        return page;*/
		LambdaQueryWrapper<EduTeacher> eduTeacherLambdaQueryWrapper=new LambdaQueryWrapper<>();
		eduTeacherLambdaQueryWrapper.orderByDesc(EduTeacher::getSort);
		if(teacherQuery==null){
			eduTeacherService.page(page, null);
			return page;
		}

		String name = teacherQuery.getName();
		Integer level = teacherQuery.getLevel();
		String begin = teacherQuery.getBegin();
		String end = teacherQuery.getEnd();
		System.out.println(begin+end);
        //查询条件
		eduTeacherLambdaQueryWrapper.eq(level!=null,EduTeacher::getLevel, level);
		eduTeacherLambdaQueryWrapper.like(name!=null,EduTeacher::getName, name);
		eduTeacherLambdaQueryWrapper.ge(begin!=null,EduTeacher::getGmtCreate, begin);// >=
		eduTeacherLambdaQueryWrapper.le(end!=null,EduTeacher::getGmtModified, end);// =<
		eduTeacherService.page(page,eduTeacherLambdaQueryWrapper);
		return page;
	}

	@Override
	public Map<String, Object> pageListWeb(Page<EduTeacher> pageParam) {

		QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("sort");

		baseMapper.selectPage(pageParam, queryWrapper);

		List<EduTeacher> records = pageParam.getRecords();
		long current = pageParam.getCurrent();
		long pages = pageParam.getPages();
		long size = pageParam.getSize();
		long total = pageParam.getTotal();
		boolean hasNext = pageParam.hasNext();
		boolean hasPrevious = pageParam.hasPrevious();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("items", records);
		map.put("current", current);
		map.put("pages", pages);
		map.put("size", size);
		map.put("total", total);
		map.put("hasNext", hasNext);
		map.put("hasPrevious", hasPrevious);

		return map;
	}

	@Override
	public String selectTeacherNameById(String teacherId) {
		return eduTeacherMapper.selectTeacherNameById(teacherId);

	}
}
